home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / malloc.lha / malloc / malloc.h < prev    next >
C/C++ Source or Header  |  1993-05-30  |  8KB  |  262 lines

  1. /* Declarations for `malloc' and friends.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.           Written May 1989 by Mike Haertel.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.
  19.  
  20.    The author may be reached (Email) at the address mike@ai.mit.edu,
  21.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  22.  
  23. #ifndef _MALLOC_H
  24.  
  25. #define _MALLOC_H    1
  26.  
  27. #ifdef _MALLOC_INTERNAL
  28.  
  29. #ifdef    HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32.  
  33. #if    defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
  34. #include <string.h>
  35. #else
  36. #ifndef memset
  37. #define    memset(s, zero, n)    bzero ((s), (n))
  38. #endif
  39. #ifndef memcpy
  40. #define    memcpy(d, s, n)        bcopy ((s), (d), (n))
  41. #endif
  42. #ifndef memmove
  43. #define    memmove(d, s, n)    bcopy ((s), (d), (n))
  44. #endif
  45. #endif
  46.  
  47. #if    defined(__GNU_LIBRARY__) || defined(__STDC__)
  48. #include <limits.h>
  49. #else
  50. #define    CHAR_BIT    8
  51. #endif
  52.  
  53. #endif    /* _MALLOC_INTERNAL.  */
  54.  
  55.  
  56. #ifdef    __cplusplus
  57. extern "C"
  58. {
  59. #endif
  60.  
  61. #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
  62. #undef    __P
  63. #define    __P(args)    args
  64. #undef    __ptr_t
  65. #define    __ptr_t        void *
  66. #else /* Not C++ or ANSI C.  */
  67. #undef    __P
  68. #define    __P(args)    ()
  69. #undef    const
  70. #define    const
  71. #undef    __ptr_t
  72. #define    __ptr_t        char *
  73. #endif /* C++ or ANSI C.  */
  74.  
  75. #ifdef    __STDC__
  76. #include <stddef.h>
  77. #else
  78. #undef    size_t
  79. #define    size_t        unsigned int
  80. #undef    ptrdiff_t
  81. #define    ptrdiff_t    int
  82. #endif
  83.  
  84. #ifndef    NULL
  85. #define    NULL    0
  86. #endif
  87.  
  88.  
  89. /* Allocate SIZE bytes of memory.  */
  90. extern __ptr_t malloc __P ((size_t __size));
  91. /* Re-allocate the previously allocated block
  92.    in __ptr_t, making the new block SIZE bytes long.  */
  93. extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
  94. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
  95. extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
  96. /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
  97. extern void free __P ((__ptr_t __ptr));
  98.  
  99. /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
  100. extern __ptr_t memalign __P ((size_t __alignment, size_t __size));
  101.  
  102. /* Allocate SIZE bytes on a page boundary.  */
  103. extern __ptr_t valloc __P ((size_t __size));
  104.  
  105.  
  106. #ifdef _MALLOC_INTERNAL
  107.  
  108. /* The allocator divides the heap into blocks of fixed size; large
  109.    requests receive one or more whole blocks, and small requests
  110.    receive a fragment of a block.  Fragment sizes are powers of two,
  111.    and all fragments of a block are the same size.  When all the
  112.    fragments in a block have been freed, the block itself is freed.  */
  113. #define INT_BIT        (CHAR_BIT * sizeof(int))
  114. #define BLOCKLOG    (INT_BIT > 16 ? 12 : 9)
  115. #define BLOCKSIZE    (1 << BLOCKLOG)
  116. #define BLOCKIFY(SIZE)    (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  117.  
  118. /* Determine the amount of memory spanned by the initial heap table
  119.    (not an absolute limit).  */
  120. #define HEAP        (INT_BIT > 16 ? 4194304 : 65536)
  121.  
  122. /* Number of contiguous free blocks allowed to build up at the end of
  123.    memory before they will be returned to the system.  */
  124. #define FINAL_FREE_BLOCKS    8
  125.  
  126. /* Data structure giving per-block information.  */
  127. typedef union
  128.   {
  129.     /* Heap information for a busy block.  */
  130.     struct
  131.       {
  132.     /* Zero for a large block, or positive giving the
  133.        logarithm to the base two of the fragment size.  */
  134.     int type;
  135.     union
  136.       {
  137.         struct
  138.           {
  139.         size_t nfree;    /* Free fragments in a fragmented block.  */
  140.         size_t first;    /* First free fragment of the block.  */
  141.           } frag;
  142.         /* Size (in blocks) of a large cluster.  */
  143.         size_t size;
  144.       } info;
  145.       } busy;
  146.     /* Heap information for a free block
  147.        (that may be the first of a free cluster).  */
  148.     struct
  149.       {
  150.     size_t size;        /* Size (in blocks) of a free cluster.  */
  151.     size_t next;        /* Index of next free cluster.  */
  152.     size_t prev;        /* Index of previous free cluster.  */
  153.       } free;
  154.   } malloc_info;
  155.  
  156. /* Pointer to first block of the heap.  */
  157. extern char *_heapbase;
  158.  
  159. /* Table indexed by block number giving per-block information.  */
  160. extern malloc_info *_heapinfo;
  161.  
  162. /* Address to block number and vice versa.  */
  163. #define BLOCK(A)    (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  164. #define ADDRESS(B)    ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
  165.  
  166. /* Current search index for the heap table.  */
  167. extern size_t _heapindex;
  168.  
  169. /* Limit of valid info table indices.  */
  170. extern size_t _heaplimit;
  171.  
  172. /* Doubly linked lists of free fragments.  */
  173. struct list
  174.   {
  175.     struct list *next;
  176.     struct list *prev;
  177.   };
  178.  
  179. /* Free list headers for each fragment size.  */
  180. extern struct list _fraghead[];
  181.  
  182. /* List of blocks allocated with `memalign' (or `valloc').  */
  183. struct alignlist
  184.   {
  185.     struct alignlist *next;
  186.     __ptr_t aligned;        /* The address that memaligned returned.  */
  187.     __ptr_t exact;        /* The address that malloc returned.  */
  188.   };
  189. extern struct alignlist *_aligned_blocks;
  190.  
  191. /* Instrumentation.  */
  192. extern size_t _chunks_used;
  193. extern size_t _bytes_used;
  194. extern size_t _chunks_free;
  195. extern size_t _bytes_free;
  196.  
  197. /* Internal version of `free' used in `morecore' (malloc.c). */
  198. extern void _free_internal __P ((__ptr_t __ptr));
  199.  
  200. #endif /* _MALLOC_INTERNAL.  */
  201.  
  202. /* Underlying allocation function; successive calls should
  203.    return contiguous pieces of memory.  */
  204. extern __ptr_t (*__morecore) __P ((ptrdiff_t __size));
  205.  
  206. /* Default value of `__morecore'.  */
  207. extern __ptr_t __default_morecore __P ((ptrdiff_t __size));
  208.  
  209. /* If not NULL, this function is called after each time
  210.    `__morecore' is called to increase the data size.  */
  211. extern void (*__after_morecore_hook) __P ((void));
  212.  
  213. /* Nonzero if `malloc' has been called and done its initialization.  */
  214. extern int __malloc_initialized;
  215.  
  216. /* Hooks for debugging versions.  */
  217. extern void (*__free_hook) __P ((__ptr_t __ptr));
  218. extern __ptr_t (*__malloc_hook) __P ((size_t __size));
  219. extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
  220.  
  221. /* Activate a standard collection of debugging hooks.  */
  222. extern int mcheck __P ((void (*__func) __P ((void))));
  223.  
  224. /* Activate a standard collection of tracing hooks.  */
  225. extern void mtrace __P ((void));
  226.  
  227. /* Statistics available to the user.  */
  228. struct mstats
  229.   {
  230.     size_t bytes_total;        /* Total size of the heap. */
  231.     size_t chunks_used;        /* Chunks allocated by the user. */
  232.     size_t bytes_used;        /* Byte total of user-allocated chunks. */
  233.     size_t chunks_free;        /* Chunks in the free list. */
  234.     size_t bytes_free;        /* Byte total of chunks in the free list. */
  235.   };
  236.  
  237. /* Pick up the current statistics. */
  238. extern struct mstats mstats __P ((void));
  239.  
  240. /* Call WARNFUN with a warning message when memory usage is high.  */
  241. extern void memory_warnings __P ((__ptr_t __start,
  242.                   void (*__warnfun) __P ((__const char *))));
  243.  
  244.  
  245. /* Relocating allocator.  */
  246.  
  247. /* Allocate SIZE bytes, and store the address in *HANDLEPTR.  */
  248. extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, size_t __size));
  249.  
  250. /* Free the storage allocated in HANDLEPTR.  */
  251. extern void r_alloc_free __P ((__ptr_t *__handleptr));
  252.  
  253. /* Adjust the block at HANDLEPTR to be SIZE bytes long.  */
  254. extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, size_t __size));
  255.  
  256.  
  257. #ifdef    __cplusplus
  258. }
  259. #endif
  260.  
  261. #endif /* malloc.h  */
  262.